/***************************************************************************
 **
 **  Copyright (C) 2021 MaMinJie <canpool@163.com>
 **  Contact: https://github.com/canpool
 **           https://gitee.com/icanpool
 **
 **  GNU Lesser General Public License Usage
 **  Alternatively, this file may be used under the terms of the GNU Lesser
 **  General Public License version 3 as published by the Free Software
 **  Foundation and appearing in the file LICENSE.LGPL3 included in the
 **  packaging of this file. Please review the following information to
 **  ensure the GNU Lesser General Public License version 3 requirements
 **  will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
 **
 **  GNU General Public License Usage
 **  Alternatively, this file may be used under the terms of the GNU
 **  General Public License version 2.0 or (at your option) the GNU General
 **  Public license version 3 or any later version approved by the KDE Free
 **  Qt Foundation. The licenses are as published by the Free Software
 **  Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
 **  included in the packaging of this file. Please review the following
 **  information to ensure the GNU General Public License requirements will
 **  be met: https://www.gnu.org/licenses/gpl-2.0.html and
 **  https://www.gnu.org/licenses/gpl-3.0.html.
 **
****************************************************************************/
#include "literibbonpage.h"
#include "literibbongroup.h"
#include "literibbongroup_p.h"

#include <QLabel>
#include <QHBoxLayout>
#include <QResizeEvent>

#include <QDebug>

QLITE_BEGIN_NAMESPACE

class LiteRibbonPagePrivate
{
public:
    LiteRibbonPagePrivate();

    void init();

    bool validateIndex(int index) const
    { return index >= 0 && index < m_listGroups.count(); }

    void insertGroup(int index, LiteRibbonGroup *group);
    int groupIndex(LiteRibbonGroup* page) const;

public:
    LiteRibbonPage *q;
    LiteRibbonGroupScrollButton *m_buttonScrollGroupLeft;
    LiteRibbonGroupScrollButton *m_buttonScrollGroupRight;
    QList<LiteRibbonGroup *> m_listGroups;
    QHBoxLayout *m_layout;
    QString m_title;
};

LiteRibbonPagePrivate::LiteRibbonPagePrivate()
    : m_title(QObject::tr("Untitled"))
{

}

void LiteRibbonPagePrivate::init()
{
    m_buttonScrollGroupLeft = new LiteRibbonGroupScrollButton(Qt::LeftArrow, q);
    m_buttonScrollGroupLeft->setVisible(false);

    m_buttonScrollGroupRight = new LiteRibbonGroupScrollButton(Qt::RightArrow, q);
    m_buttonScrollGroupRight->setVisible(false);

    m_layout = new QHBoxLayout();
    m_layout->setSpacing(1);
    m_layout->setMargin(0);

    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->setSpacing(0);
    mainLayout->setMargin(0);
    mainLayout->addLayout(m_layout);
    mainLayout->addStretch();

    q->setLayout(mainLayout);
}

void LiteRibbonPagePrivate::insertGroup(int index, LiteRibbonGroup *group)
{
    if (validateIndex(index)) {
        m_listGroups.insert(index, group);
        m_layout->insertWidget(index, group);
    } else {
        m_listGroups.append(group);
        m_layout->addWidget(group);
    }
}

LiteRibbonPage::LiteRibbonPage(QWidget *parent)
    : QWidget(parent)
    , d(new LiteRibbonPagePrivate())
{
    d->q = this;
    d->init();
    setObjectName(QLatin1String("qlite_ribbonpage"));
}

LiteRibbonPage::~LiteRibbonPage()
{

}

void LiteRibbonPage::addGroup(LiteRibbonGroup *group)
{
    insertGroup(-1, group);
}

LiteRibbonGroup *LiteRibbonPage::addGroup(const QString &title)
{
    return insertGroup(-1, title);
}

LiteRibbonGroup *LiteRibbonPage::addGroup(const QIcon &icon, const QString &title)
{
    return insertGroup(-1, icon, title);
}

void LiteRibbonPage::insertGroup(int index, LiteRibbonGroup *group)
{
    Q_ASSERT(group);
    group->setParent(this);
    d->insertGroup(index, group);
}

LiteRibbonGroup *LiteRibbonPage::insertGroup(int index, const QString &title)
{
    LiteRibbonGroup *group = new LiteRibbonGroup(title);
    insertGroup(index, group);
    return group;
}

LiteRibbonGroup *LiteRibbonPage::insertGroup(int index, const QIcon &icon, const QString &title)
{
    if (LiteRibbonGroup* group = insertGroup(index, title)) {
        group->setIcon(icon);
        return group;
    }
    return Q_NULLPTR;
}

void LiteRibbonPage::removeGroup(LiteRibbonGroup *group)
{

}

void LiteRibbonPage::removeGroupByIndex(int index)
{

}

void LiteRibbonPage::detachGroup(LiteRibbonGroup *group)
{

}

void LiteRibbonPage::detachGroupByIndex(int index)
{

}

void LiteRibbonPage::clearGroups()
{

}

QAction *LiteRibbonPage::defaultAction() const
{
    return Q_NULLPTR;
}

int LiteRibbonPage::groupCount() const
{
    return 0;
}

LiteRibbonGroup *LiteRibbonPage::groupAt(int index) const
{
    return Q_NULLPTR;
}

int LiteRibbonPage::groupIndex(LiteRibbonGroup *group) const
{
    return -1;
}

QList<LiteRibbonGroup *> LiteRibbonPage::groups() const
{
    QList<LiteRibbonGroup *> lst;
    return lst;
}

const QString &LiteRibbonPage::title() const
{
    return d->m_title;
}

void LiteRibbonPage::setTitle(const QString &title)
{
    if (d->m_title == title) {
        return;
    }
    d->m_title = title;
    emit titleChanged(d->m_title);
}

void LiteRibbonPage::resizeEvent(QResizeEvent *event)
{
    QSize delta = event->size() - event->oldSize();
    if (delta.isNull()) {
        return;
    }
    qDebug() << event->size();
}

QLITE_END_NAMESPACE
